package dao.implementacao.jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import dao.ConnectionPool;
import dao.FabricaDAO;
import dao.assinatura.IDiciplinaDAO;


import model.Aluno;
import model.Diciplina;

public class DiciplinaDAO implements IDiciplinaDAO{

	private DataSource dataSource;
	
	public DiciplinaDAO() {
		ConnectionPool connectionPool = FabricaDAO.createCoonectionPool();
		this.dataSource = connectionPool.getDataSource();
	}
	

	public void addDiciplina(Diciplina diciplina) {
		try 
        {  
			Connection con = dataSource.getConnection();
		 	String sql = "insert into diciplina(cod_dic,nome,peso,cod_ins_dic) values(?,?,?,?)";
		 	PreparedStatement comando = con.prepareStatement(sql);
		 	comando.setInt(1, diciplina.getCodigo());
            comando.setString(2, diciplina.getNome());
            comando.setInt(3, diciplina.getPeso());
            comando.setInt(4, diciplina.getInstituicao());
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }		
		
	}

	@Override
	public void updateDiciplina(Diciplina diciplina) 
	{
		try 
        {  
			Connection con = dataSource.getConnection();
			String sql = "update diciplina set nome=? , peso = ?, cod_ins_dic = ? where cod_dic=?";
		 	PreparedStatement comando = con.prepareStatement(sql);
            comando.setString(1, diciplina.getNome());
            comando.setInt(2, diciplina.getPeso());
            comando.setInt(3, diciplina.getInstituicao());
            comando.setInt(4, diciplina.getCodigo());    
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }	
	}

	@Override
	public void deleteDiciplina(Diciplina diciplina) 
	{
		try 
        {  
			Connection con = dataSource.getConnection();
		 	String sql = "delete from diciplina where cod_dic=?";
		 	PreparedStatement comando = con.prepareStatement(sql);
            comando.setInt(1, diciplina.getCodigo());
            comando.execute();
            comando.close();
        } catch (Exception e) 
        {  
        	e.printStackTrace();
        }		
		
	}

	@Override
	public Diciplina getDiciplinaById(int id) 
	{
		Diciplina retorno = null;
		
		try 
		{		
			String sql = "select * from diciplina where cod_dic ="+id;
			Connection con = dataSource.getConnection();
			Statement s = con.createStatement();	
			ResultSet rs = s.executeQuery(sql);	
			
			if(rs.next()){
				int codigo = rs.getInt("cod_dic");
				String nome = rs.getString("nome");
				int peso = rs.getInt("peso");
				
				retorno = new Diciplina(codigo,nome,peso);
			}
			
			s.close();
			rs.close();
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		
		return retorno;
	}

	@Override
	public List<Diciplina> getDiciplinas() 
	{
		List<Diciplina> diciplina = new ArrayList<Diciplina>();
		try 
		{		
			String sql = "select * from diciplina";
			Connection con = dataSource.getConnection();
			Statement s = con.createStatement();	
			ResultSet rs = s.executeQuery(sql);	
			
			while(rs.next())
			{
				int codigo = rs.getInt("cod_alu");
				String nome = rs.getString("nome");
				int peso = rs.getInt("peso");
				
				
				diciplina.add(new Diciplina(codigo,nome,peso));
			}
			
			s.close();
			rs.close();
			
		} catch (SQLException e) 
		{
			e.printStackTrace();
		}	
		
		return diciplina;
	}

	@Override
	public void finalizaConexaoDiciplina() {
		try {
			Connection con = dataSource.getConnection();
			con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
